<?
require_once("mvc/action/Action.php");

class SaveAction extends Action {

    private $log;

    public function __construct() {
        $this->log =& LoggerManager::getLogger("SaveAction");
    }

    public function execute(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $errors = new ActionMessages();

        $topic = $form->getTopic();
        $topic->setModifiedDate(date(_Core::FORMAT_SQLDATETIME));

        $topic->setBody(strip_tags($topic->getBody(), '<p><a><b><i><u><strong><img><br><strike>'));

        $conn = Propel::getConnection();

        try {

            $conn->begin();

            if($topic->getTopicId() > 0) {

                $topic->setTopicId($topic->getTopicId());

                // Update the topic
                TopicPeer::doUpdate($topic, $conn);

            } else {

                $topic->setInsertDate(date(_Core::FORMAT_SQLDATETIME));
                $topic->setUsername($request->getUserPrincipal()->getName());
                $topic->setActive(_Core::AUTO_ACTIVATE);

                TopicPeer::doInsert($topic, $conn);

            }

            $conn->commit();

        } catch(PropelException $e) {
            $conn->rollback();
            $errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("error.save", true));
            $this->log->error($e->getMessage());
        }

        // Return any save errors.
        if(!$errors->isEmpty()) {
            $this->saveErrors($request, $errors);
            return $mapping->getInputForward();
        }


        // Reset the topicListForm from & to session.
        $session  = $request->getSession();
        $listForm = $session->getAttribute("cms/topicListForm");
        $listForm->setPage(0);
        $session->setAttribute("cms/topicListForm", $listForm);


        // To the success topic.
        return $mapping->findForward("success");

    }

}
?>